window.onload = function() {

    var url = '/order/index';
    var config = {
        elem: "#userTable",
        url: url,
        cols: [
            [
                { type: 'numbers', width: '5%', title: '编号', sort:true },
                { field: 'order_no', width: '18%', title: '订单编号' },
                {
                    field: 'created_time',
                    width: '15%',
                    align: 'center',
                    title: '下单时间',
                    templet: function(item) {
                        return timestampToTime(parseInt(item.created_time));
                    }
                },
                { field: 'order_money', width: '5%', align: 'center', title: '金额' , sort:true },
                {
                    field: 'order_status',
                    width: '5%',
                    align: 'center',
                    title: '状态',
                    templet: function(item) {
                        return getStatus(parseInt(item.order_status));
                    }
                },
                { field: 'addressee_name', width: '7%', align: 'center', title: '用户姓名' },
                { field: 'addressee_phone', width: '10%', align: 'center', title: '手机号' },
                { field: 'sku', width: '15%', align: 'center', title: 'SKU' },
                { field: 'remark', width: '10%', align: 'center', title: '订单备注' },
                { fixed: 'right', width: '10%', align: 'center', toolbar: '#operation', title: '操作' }
            ]
        ],
        page: true
    };
    layui.use(['table', 'laydate', 'form', 'layer'], function() {
        var table = layui.table,
            form = layui.form,
            $ = layui.$,
            layer = layui.layer,
            laydate = layui.laydate,
            serch = $('#serch-form').serialize();
            config.url = '/order/index?'+serch;
        table.render(config);
        //监听工具条
        table.on('tool(operation)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'detail') { //查看
                location.href = '/order/get-info?id=' + data.id;
            } else if (layEvent === 'edit') {
                location.href = '/order/update-order?id=' + data.id;
            } else if (layEvent === 'send') {
                fahuo()
            } else if (layEvent === 'refund') {
                Utils.layer.msg('退款操作', {
                    time: 0,
                    btn: ['继续发货', '退款', '关闭'],
                    btn1: function() {
                        fahuo()
                    },
                    btn2: function() {
                        Utils.layer.msg('是否确认退款', {
                            time: 0,
                            btn: ['确认退款', '关闭'],
                            btn1: function() {
                                $.ajax({
                                    'type': 'POST',
                                    'url': '/order/refund-goods',
                                    'data': {
                                        id: data.id,
                                    },
                                    'dataType': 'json',
                                    'success': function(data) {
                                        Utils.layer.msg(data.msg);
                                        return;
                                        if (data.code == 200) {
                                            setTimeout(function() {
                                                location.href = '/order/index';
                                            }, 1200);
                                        }
                                    }
                                });
                            }
                        })
                    }
                })
            }
        });

        function fahuo() {
            var htmlStr = function() {
                var expdata = '';
                var exp = '';
                $.ajax({
                    'type': 'POST',
                    'url': '/order/get-express',
                    'dataType': 'json',
                    'async': false,
                    'success': function(res) {
                        expdata = res.data;
                        var newexp = '<option value="">请选择快递公司</option>';
                        $.each(expdata, function(index, domEle) {
                            newexp += '<option value="' + domEle.code + '">' + domEle.express_name + '</option>'
                        });
                        exp = '<select class="exp" style=" display:block; height:38px; border-color:#e6e6e6; margin: 10px auto 0;width:90%; color: #999;">' + newexp + '</select><input type="test" class="transport_two layui-input lauyi-input-inline" style="width: 90%; margin: 10px auto 0;" placeholder="请输入运单号">';
                    }
                });
                return exp;
            }
            Utils.layer.open({
                id: 1,
                type: 1,
                title: '输入运单号',
                skin: 'layui-layer-rim',
                area: ['320px', 'auto'],
                content: htmlStr(),
                btn: ['确认发货', '取消'],
                btn1: function(index, layero) {
                    var transport_two = $('.transport_two').val();
                    var expval = $('.exp').val();
                    if (expval == "") {
                        Utils.layer.msg('请选择物流公司');
                        return;
                    } else if (transport_two == "") {
                        Utils.layer.msg('请输入运单号');
                        return;
                    } else {
                        $.ajax({
                            'type': 'POST',
                            'url': '/order/deliver-goods',
                            'data': { id: data.id, transport_two: transport_two, express: expval },
                            'dataType': 'json',
                            'success': function(data) {
                                Utils.layer.msg(data.msg)
                                if (data.code == 200) {
                                    setTimeout(function() {
                                        location.href = '/order/index';
                                    }, 1200);
                                }

                            }
                        });
                    }
                },
                btn2: function(index, layero) {
                    Utils.layer.closeAll();
                }
            });
        }
        laydate.render({
            elem: '#start_time'
        });
        laydate.render({
            elem: '#end_time'
        });
        //搜索
        form.on("submit(serch-form)", function(data) {
            var addressee_name = $('#addressee_name').val();
            var search_type = $('.search_type option:selected').val();
            var order_status = $('.order_status option:selected').val();
            var start_time = $('#start_time').val();
            var end_time = $('#end_time').val();
            var url = '/order/index/?addressee_name=' + addressee_name + '&search_type=' + search_type + '&order_status=' + order_status + '&start_time=' + start_time + '&end_time=' + end_time;
            config.url = url;
            table.render(config);
            return false;
        });
    })
}